package GradeManager.StudentGradeSystem.service.imp;

import GradeManager.StudentGradeSystem.common.BaseContent;
import GradeManager.StudentGradeSystem.dao.StudentDao;
import GradeManager.StudentGradeSystem.pojo.LoginDTO;
import GradeManager.StudentGradeSystem.pojo.PageBean;
import GradeManager.StudentGradeSystem.pojo.Student;
import GradeManager.StudentGradeSystem.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class StudentServiceImp implements StudentService {
    @Autowired
    private StudentDao studentDao;
    @Override
    public PageBean<Student> initTable(int currentPage, int pageSize) {
        LambdaQueryWrapper<Student> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Student::getStudentId,BaseContent.getLoginClaims().get("loginId"));
//        根据页码和条数分页
        IPage page=new Page(currentPage,pageSize);
        studentDao.selectPage(page, wrapper);
        //封装pageBean对象并返回
        PageBean<Student> pageBean=new PageBean<>();
        pageBean.setRows(page.getRecords());
        pageBean.setTotalCount(page.getTotal());
        return pageBean;
    }

    @Override
    public PageBean<Student> searchByCondition(Student studentCondition,int currentPage,int pageSize) {
        String subject=studentCondition.getSubjectName();
        String schoolyear=studentCondition.getSchoolYear();
//        根据年份和科目名搜索成绩记录
        LambdaQueryWrapper<Student> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasLength(subject),Student::getSubjectName,subject)
                .eq(StringUtils.hasLength(schoolyear),Student::getSchoolYear,schoolyear)
                .eq(Student::getStudentId,BaseContent.getLoginClaims().get("loginId"));
        //对搜索结果进行分页
        IPage page=new Page(currentPage,pageSize);
        studentDao.selectPage(page,wrapper);
        //封装pageBean对象
        PageBean<Student> pageBean=new PageBean<>();
        pageBean.setRows(page.getRecords());
        pageBean.setTotalCount(page.getTotal());
        return pageBean;
    }
}
